// 引入常态化样式
require("../../assets/css/normalize.css");
// 引入公共样式
require("../../assets/css/common.css");
// 引入自己的样式
require("./login.less");
// 引入阿里矢量图
require("../../assets/fonts/iconfont.css");
//引入公共js
const dom = require("../../utils/dom.js");

const axios = require("axios");

//登录验证
document.addEventListener("DOMContentLoaded", function () {
	dom.get("#btn").addEventListener("click", function () {
		// 账号验证
		let account = dom.get("#account").value;
		const regex = /^1[3456789]\d{9}$/i;
		if (regex.test(account)) {
			dom.get(".erron").textContent = " ";
		} else {
			dom.get(".erron").textContent = "账号输入有误";
			return;
		}

		//密码验证
		let password = dom.get("#password").value;
		if (password.length >= 6) {
			dom.get(".erron").textContent = " ";
		} else {
			dom.get(".erron").textContent = "密码不得少于6位";
			return;
		}

		//通过验证，发送请求
		axios
			.post(" http://fitness.h5.itsource.cn:3701/api/user/login", {
				account: account,
				password: password,
			})
			.then((res) => {
				if (res.data.errno === 0) {
					localStorage.setItem("token", res.data.data.token); //将token存储起来
					location.href = "index.html";
				} else {
					alert(res.data.message);
				}
			});
	});
});
